import time
from Case.python_ui.Common.basepage import Basepage
from Case.python_ui.PageLocate.admin_order_management_locator import OrderManagementLocator
from Case.python_ui.Test_data.organization_data import Organization


class OrderManagmentPage(Basepage):

    def enter_ordermanagement_page(self):
        doc = '进入到ordermanagement页面'
        for i in range(3):
            try:
                eles = self.get_elements(OrderManagementLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(OrderManagementLocator.order_management_tab, doc)
                self.click_element(OrderManagementLocator.order_management_tab, doc)
                self.wait_elevisble(OrderManagementLocator.order_tab, doc)
                self.click_element(OrderManagementLocator.order_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(OrderManagementLocator.page_total,doc)


    def page_default_display(self):
        doc='进入页面默认展示'
        self.enter_ordermanagement_page()
        default_acct_type=self.get_element(OrderManagementLocator.defaluat_acct_type,doc).text
        return default_acct_type


    def bp_type_search(self):
        doc='bp类型查询'
        self.enter_ordermanagement_page()
        self.click_element(OrderManagementLocator.acct_type_select,doc)
        self.wait_elevisble(OrderManagementLocator.bp_chooice,doc)
        self.click_element(OrderManagementLocator.bp_chooice,doc)
        self.wait_elevisble(OrderManagementLocator.page_total,doc)
        order_list=self.get_elements(OrderManagementLocator.order_id,doc)
        order_id=order_list[0].text
        return order_id


    def search_by_bp_name(self,cmdopt):
        doc='根据bp名称查询'
        self.enter_ordermanagement_page()
        self.click_element(OrderManagementLocator.acct_type_select, doc)
        self.wait_elevisble(OrderManagementLocator.bp_chooice, doc)
        self.click_element(OrderManagementLocator.bp_chooice, doc)
        self.send_keys(OrderManagementLocator.company_name_input,Organization.bp_name[cmdopt],doc)
        time.sleep(2)
        self.keyboard_event_enter(OrderManagementLocator.company_name_input,doc)
        self.wait_elevisble(OrderManagementLocator.bp_data,doc)
        bp_data=self.get_elements(OrderManagementLocator.bp_data,doc)
        return bp_data

    def search_with_multiple_search_criteria(self,cmdopt):
        doc = '联合查询'
        self.enter_ordermanagement_page()
        self.click_element(OrderManagementLocator.acct_type_select, doc)
        self.wait_elevisble(OrderManagementLocator.bp_chooice, doc)
        self.click_element(OrderManagementLocator.bp_chooice, doc)
        self.send_keys(OrderManagementLocator.company_name_input, Organization.bp_name[cmdopt], doc)
        time.sleep(2)
        self.keyboard_event_enter(OrderManagementLocator.company_name_input, doc)
        self.wait_elevisble(OrderManagementLocator.bp_data, doc)
        bp_data = self.get_elements(OrderManagementLocator.bp_data, doc)
        return bp_data


    def refresh_data_list(self):
        doc='刷新按钮'
        self.enter_ordermanagement_page()
        self.click_element(OrderManagementLocator.refresh_button,doc)
        data = self.get_elements(OrderManagementLocator.bp_data, doc)
        return data


    def order_detail(self):
        doc='查看order详情'
        self.enter_ordermanagement_page()
        self.wait_elevisble(OrderManagementLocator.order_id_list,doc)
        order_id_list=self.get_elements(OrderManagementLocator.order_id_list,doc)
        order_id=order_id_list[0].text
        order_id_list[0].click()
        self.wait_elevisble(OrderManagementLocator.detail_order_id,doc)
        detail_order_id=self.get_element(OrderManagementLocator.detail_order_id,doc).text
        return order_id,detail_order_id


    def download_excle_report(self):
        doc='订单页面下载功能'
        self.enter_ordermanagement_page()
        self.wait_elevisble(OrderManagementLocator.start_time,doc)
        self.send_keys(OrderManagementLocator.start_time,'01-01-2023',doc)
        time.sleep(2)
        self.send_keys(OrderManagementLocator.end_time,'01-02-2023',doc)
        time.sleep(2)
        self.keyboard_event_enter(OrderManagementLocator.end_time,doc)
        self.click_element(OrderManagementLocator.down_load_button,doc)



    def download_pdf_report(self):
        doc = '订单详情下载功能'
        self.enter_ordermanagement_page()
        self.wait_elevisble(OrderManagementLocator.view_detail_button,doc)
        view_buttons=self.get_elements(OrderManagementLocator.view_detail_button,doc)
        view_buttons[0].click()
        self.wait_elevisble(OrderManagementLocator.download_button,doc)
        time.sleep(2)
        self.click_element(OrderManagementLocator.download_button,doc)




    def enter_dunning_methods_page(self):
        doc = '进入到dunning methods页面'
        for i in range(3):
            try:
                eles = self.get_elements(OrderManagementLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(OrderManagementLocator.credit_control_tab, doc)
                self.click_element(OrderManagementLocator.credit_control_tab, doc)
                self.wait_elevisble(OrderManagementLocator.dunning_methods_tab, doc)
                self.click_element(OrderManagementLocator.dunning_methods_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(OrderManagementLocator.grace_period, doc)



    def prepay_customer_dunning_edit(self):
        doc='编辑prepay_customer的dunning方式'
        self.enter_dunning_methods_page()
        env_value=self.get_element(OrderManagementLocator.grace_period,doc).get_attribute('value')
        self.keyboard_event(OrderManagementLocator.grace_period,doc)
        self.send_keys(OrderManagementLocator.grace_period,'99',doc)
        time.sleep(2)
        self.wait_elevisble(OrderManagementLocator.zar,doc)
        self.scroll_bar_operation(OrderManagementLocator.zar)
        self.move_mouse_to_click_element(OrderManagementLocator.zar,doc)
        for i in range(2):
            try:
                time.sleep(1)
                self.wait_elevisble(OrderManagementLocator.down, doc)
                self.move_mouse_to_click_element(OrderManagementLocator.down, doc)
                time.sleep(2)
                self.click_element(OrderManagementLocator.download_button, doc)
                time.sleep(2)
                self.wait_elevisble(OrderManagementLocator.please_enter,doc)
                continue
            except:
                break
        time.sleep(2)
        # self.keyboard_event(OrderManagementLocator.grace_period,doc)
        # time.sleep(2)
        # self.send_keys(OrderManagementLocator.grace_period,env_value, doc)
        # self.click_element(OrderManagementLocator.download_button,doc)
        # self.wait_elevisble(OrderManagementLocator.success_prompt,doc)


    def postpaid_customer_dunning_edit(self):
        doc = '编辑postpaid_customer的dunning方式'
        self.enter_dunning_methods_page()
        time.sleep(2)
        self.click_element(OrderManagementLocator.postpaid_customer_tab,doc)
        self.wait_elevisble(OrderManagementLocator.postpaid_grace_period, doc)
        env_value = self.get_element(OrderManagementLocator.postpaid_grace_period, doc).get_attribute('value')
        self.keyboard_event(OrderManagementLocator.postpaid_grace_period, doc)
        time.sleep(2)
        self.send_keys(OrderManagementLocator.postpaid_grace_period,'99',doc)
        # self.click_element(OrderManagementLocator.download_button, doc)
        # self.wait_elevisble(OrderManagementLocator.success_prompt, doc)
        for i in range(2):
            try:
                time.sleep(1)
                self.wait_elevisble(OrderManagementLocator.down, doc)
                self.move_mouse_to_click_element(OrderManagementLocator.down, doc)
                time.sleep(2)
                self.click_element(OrderManagementLocator.download_button, doc)
                time.sleep(2)
                self.wait_elevisble(OrderManagementLocator.please_enter, doc)
                continue
            except:
                break
        time.sleep(2)
        self.click_element(OrderManagementLocator.postpaid_customer_tab, doc)
        self.keyboard_event(OrderManagementLocator.postpaid_grace_period, doc)
        time.sleep(2)
        self.send_keys(OrderManagementLocator.postpaid_grace_period, env_value, doc)
        for i in range(2):
            try:
                time.sleep(1)
                self.wait_elevisble(OrderManagementLocator.down, doc)
                self.move_mouse_to_click_element(OrderManagementLocator.down, doc)
                time.sleep(2)
                self.click_element(OrderManagementLocator.download_button, doc)
                time.sleep(2)
                self.wait_elevisble(OrderManagementLocator.please_enter, doc)
                continue
            except:
                break
        time.sleep(2)
        # self.click_element(OrderManagementLocator.download_button, doc)
        # self.wait_elevisble(OrderManagementLocator.success_prompt, doc)

    def enter_expense_contorl_page(self):
        doc = '进入到expense control页面'
        for i in range(3):
            try:
                eles = self.get_elements(OrderManagementLocator.default_page_expanded, doc)
                for ele in eles:
                    ele.click()
                    time.sleep(1)
                self.wait_elevisble(OrderManagementLocator.credit_control_tab, doc)
                self.click_element(OrderManagementLocator.credit_control_tab, doc)
                self.wait_elevisble(OrderManagementLocator.expense_control_tab, doc)
                self.click_element(OrderManagementLocator.expense_control_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(OrderManagementLocator.fixed_value_input, doc)


    def eneter_my_resource_page(self):
        doc='进入user端的my resource页面'
        for i in range(3):
            try:
                self.wait_elevisble(OrderManagementLocator.user_my_resource_tab, doc)
                self.click_element(OrderManagementLocator.user_my_resource_tab, doc)
                break
            except:
                self.refresh_page()
                continue
        time.sleep(5)
        self.wait_elevisble(OrderManagementLocator.user_resource_create_button, doc)




    def get_fixed_value(self):
        doc='获取fixed的值'
        self.enter_expense_contorl_page()
        self.click_element(OrderManagementLocator.postpaid_customer_tab,doc)
        fixed_value=int(self.get_element(OrderManagementLocator.fixed_value_input,doc).get_attribute('value'))
        return  fixed_value


    def get_prepaid_fixed_value(self):
        doc = '获取prepay fixed的值'
        self.enter_expense_contorl_page()
        fixed_value = int(self.get_element(OrderManagementLocator.fixed_value_input, doc).get_attribute('value'))
        return fixed_value


    def check_buy_button(self):
        doc='检查buy按钮是否可以点击'
        self.eneter_my_resource_page()
        self.move_mouse_to_element(OrderManagementLocator.user_resource_create_button)
        time.sleep(2)
        self.click_product_by_prodouct_name('Block Storage')
        time.sleep(2)
        windows=self.driver.window_handles
        self.driver.switch_to.window(windows[-1])
        self.wait_elevisble(OrderManagementLocator.download_button,doc)
        time.sleep(5)
        buy_button_ele=self.get_element(OrderManagementLocator.download_button,doc)
        if buy_button_ele.is_enabled():
            return True
        else:
            return False



    def click_product_by_prodouct_name(self,prod_name):
        doc='通过产品名称点击'
        eles=self.get_elements(OrderManagementLocator.product_button,doc)
        time.sleep(1)
        for ele in eles:
            print(ele.text)
            if ele.text == prod_name:
               ele.click()